#####################################
# Author: Trevor Osaki              #
# Purpose: ACS Percentages          #
# Date: December 14, 2020           #
#####################################

# install.packages("haven") 
library(haven)  

# set up the work directory to access files 

# setwd("C:/Users/trevo/OneDrive/Documents/UCSB/Research Projects/When is discrimination unfair/Data/GSS Weights")
setwd("/Users/leiyue/Desktop/Projects/WhenIsDicrimUnfair/Replication_Final/")

gss = read.csv("1_Dataset Construction/jj_Materials for Weights/gss_data.csv")


# num_age = seq(1,3);
# num_edu = seq(1,2);
num_views = seq(1,7);
# num_race = seq(1,3);

shares = matrix(0,7,2)

N = sum(gss$wtssnr_2);
n = 1;

for(i in 1:length(num_views)){
  for(m in 1:nrow(gss)){
    if(gss$views2[m]==i){
            shares[n,1]<-shares[n,1] + gss$wtssnr_2[m];
    }
   }
    shares[n,1]<-shares[n,1]/N
    shares[n,2]<-i;
    n = n + 1;
}

sum(shares[,1])

shares <-data.frame(shares)
names(shares)[names(shares) == "X1"]<- "survey_weight2"
names(shares)[names(shares) == "X2"]<- "views2"

shares$survey_weight2[1]<-shares$survey_weight2[1]/0.1012461
shares$survey_weight2[2]<-shares$survey_weight2[2]/0.1635514
shares$survey_weight2[3]<-shares$survey_weight2[3]/0.0919003
shares$survey_weight2[4]<-shares$survey_weight2[4]/0.1697819
shares$survey_weight2[5]<-shares$survey_weight2[5]/0.0950156
shares$survey_weight2[6]<-shares$survey_weight2[6]/0.2741433
shares$survey_weight2[7]<-shares$survey_weight2[7]/0.1043614

write_dta(shares, "1_Dataset Construction/ee_gss_weights.dta")